#include "options.h"
#include "ui_options.h"

Options::Options(QWidget *parent) : QDialog(parent), ui(new Ui::Options) {
    ui->setupUi(this);
    for (QSerialPortInfo &port : QSerialPortInfo::availablePorts()) {
        ui->comboPort->addItem(port.portName());
    }

    ui->comboBaud->addItem("1200");
    ui->comboBaud->addItem("2400");
    ui->comboBaud->addItem("4800");
    ui->comboBaud->addItem("9600");
    ui->comboBaud->addItem("19200");
    ui->comboBaud->addItem("38400");
    ui->comboBaud->addItem("57600");
    ui->comboBaud->addItem("115200");
    /* And some not-so-standard */
    ui->comboBaud->addItem("128000");
    ui->comboBaud->addItem("153600");
    ui->comboBaud->addItem("230400");
    ui->comboBaud->addItem("256000");
    ui->comboBaud->addItem("460800");
    ui->comboBaud->addItem("921600");
}

Options::~Options() { delete ui; }

QString Options::portName() { return ui->comboPort->currentText(); }

QString Options::portSpeed() { return ui->comboBaud->currentText(); }

bool Options::saveStreamFlag() { return ui->saveStream->isChecked(); }

void Options::setOptions(QString &port, QString &speed, bool saveStream) {
    int portIndex = ui->comboPort->findText(port);
    int speedIndex = ui->comboBaud->findText(speed);
    if (portIndex >= 0 && speedIndex >= 0) {
        ui->comboPort->setCurrentIndex(portIndex);
        ui->comboBaud->setCurrentIndex(speedIndex);
    }
    ui->saveStream->setChecked(saveStream);
}

void Options::on_refreshBtn_clicked() {
    ui->comboPort->clear();
    for (QSerialPortInfo &port : QSerialPortInfo::availablePorts()) {
        ui->comboPort->addItem(port.portName());
    }
}
